package com.net.service;

import com.net.dao.BaseDao;
import com.net.domain.User;
import com.net.reflection.ReflectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang yufei on 2017/11/24.
 */
@Service
public class BaseService<T>{

    public BaseService(){

    }

    @Autowired
    BaseDao baseDao;

    public List<Map<String,Object>> select(){
        String sql="select * from user";
        List<Map<String,Object>> list=baseDao.select(sql);
        return list;
    }

    public User getUser(String name,String password){
        String sql="select * from user where name='"+name+"' and password='"+password+"'";
        List<Map<String,Object>> list=baseDao.select(sql);
        if (list.size()>0){
            User user=new User();
            user.setId(Integer.parseInt(list.get(0).get("id").toString()));
            user.setName(list.get(0).get("name").toString());
            return user;
        }else {
            return null;
        }
    }

    public List<User> getAllUser(){
        String sql="select * from user";
        List<Map<String,Object>> list=baseDao.select(sql);
        List<User> result=new ArrayList<>();
        for (Map<String,Object> map:list){
            User user= ReflectionUtil.mapToEntity(map,User.class);
            result.add(user);
        }
        return result;
    }
}


